/*
 * Created on 2005-12-27
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.sunnos.express.common;

import java.sql.Timestamp;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


/**
 * @author lifeng
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class DateUtils extends Object {
	//private static final Log log = LogFactory.getLog(DateUtils.class);

	public static String DATE_FORMAT_WITHOUT_TIME = "yyyy-MM-dd";
	public static String DATE_FORMAT_WITH_TIME = "yyyy-MM-dd-HH-mm-ss";
	public static String DATE_FORMAT_WITH_TIME_NOFLAG = "yyyyMMddHHmmss";
	private static SimpleDateFormat FORMATTER_WITH_TIME = new SimpleDateFormat(
			DATE_FORMAT_WITH_TIME);
	private static SimpleDateFormat FORMATTER_WITH_TIME_NO_FLAG = new SimpleDateFormat(
			DATE_FORMAT_WITH_TIME_NOFLAG);
	private static SimpleDateFormat FORMATTER_WITHOUT_TIME = new
	SimpleDateFormat(DATE_FORMAT_WITHOUT_TIME);
	

	/**
	 * Constructor
	 */
	private DateUtils() {
	}

	/**
	 * Whether the two dates are the same day
	 */
	public static boolean isSameDay(java.util.Date date1, java.util.Date date2) {
		if ( (date1 == null) || (date2 == null)) {
			return false;
		}

		Calendar cal1 = Calendar.getInstance();
		cal1.setTime(date1);

		Calendar cal2 = Calendar.getInstance();
		cal2.setTime(date2);

		return ( (cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)) &&
				(cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH)) &&
				(cal1.get(Calendar.DAY_OF_MONTH) ==
					cal2.get(Calendar.DAY_OF_MONTH)));
	}

	/**
	 * Get the day of yesterday.
	 */
	public static java.util.Date getYesterday(java.util.Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);

		cal.add(Calendar.DATE, -1);

		/* @old This is a stupid way to get yesterday
                if (cal.get(Calendar.MONTH) == 0 &&
                        cal.get(Calendar.DATE) == 1 ) {
                    // 2000-1-1's yesterday is 1999-12-31
                    cal.roll(Calendar.YEAR, -1);
                }
                cal.roll(Calendar.DAY_OF_YEAR, -1);
		 */
		return cal.getTime();
	}

	/**
	 * Get the day of tomorrow.
	 */
	public static java.util.Date getTomorrow(java.util.Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, 1);

		/* @old This is a stupid way to get tomorrow
                if (cal.get(Calendar.MONTH) == 11 &&
                        cal.get(Calendar.DATE) == 31 ) {
                    // 2000-12-31's tomorrow is 2001-1-1
                    cal.roll(Calendar.YEAR, 1);
                }
                cal.roll(Calendar.DAY_OF_YEAR, 1);
		 */
		return cal.getTime();
	}
	
	/**
	 * Get the day of tomorrow.
	 */
	public static java.util.Date getAfterMonth(java.util.Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, 1);

		/* @old This is a stupid way to get tomorrow
                if (cal.get(Calendar.MONTH) == 11 &&
                        cal.get(Calendar.DATE) == 31 ) {
                    // 2000-12-31's tomorrow is 2001-1-1
                    cal.roll(Calendar.YEAR, 1);
                }
                cal.roll(Calendar.DAY_OF_YEAR, 1);
		 */
		return cal.getTime();
	}

	public static int getYear(java.util.Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);

		return cal.get(Calendar.YEAR);
	}

	/**
	 * 1~12, Not 0~11
	 */
	public static int getMonth(java.util.Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);

		return cal.get(Calendar.MONTH) + 1;
	}

	public static int getDay(java.util.Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);

		return cal.get(Calendar.DATE);
	}

	public static int getHour(java.util.Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);

		return cal.get(Calendar.HOUR_OF_DAY);
	}

	public static int getMinute(java.util.Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);

		return cal.get(Calendar.MINUTE);
	}

	public static int getSecond(java.util.Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);

		return cal.get(Calendar.SECOND);
	}

	// month :1~12
	public static java.util.Date getDate(int year, int month, int day) {
		return getDate(year, month, day, 0, 0, 0);
	}

	// month :1~12
	public static java.util.Date getDate(int year, int month, int day,
			int hour, int minute, int second) {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, month - 1);
		cal.set(Calendar.DATE, day);
		cal.set(Calendar.HOUR_OF_DAY, hour);
		cal.set(Calendar.MINUTE, minute);
		cal.set(Calendar.SECOND, second);

		return cal.getTime();
	}

	// turn a date into a string, not including the portion of time
	// is passed in param is null, return ""
	public static String formatDateWithoutTime(java.util.Date date) {
		if (date == null) {
			return "";
		}

		return FORMATTER_WITHOUT_TIME.format(date);
	}

	// turn a date into a string, including the portion of time
	// is passed in param is null, return ""
	public static String formatDateWithTime(java.util.Date date) {
		if (date == null) {
			return "";
		}

		return FORMATTER_WITH_TIME.format(date);
	}
	
	public static String formatDateWithTimeNoFlag(java.util.Date date) {
		if (date == null) {
			return "";
		}

		return FORMATTER_WITH_TIME_NO_FLAG.format(date);
	}

	// format a Timestamp as string
	// if passed in param is null, return "";
	public static String formatTimestamp(Timestamp stamp) {
		if (stamp == null) {
			return "";
		}

		return stamp.toString();
	}

	// turn a string date into a Date object
	// if passed in param is null/"", return null
	public static java.util.Date parseDateWithTime(String strDate) {
		if ( (strDate == null) || "".equals(strDate)) {
			return null;
		}

		return FORMATTER_WITH_TIME.parse(strDate, new ParsePosition(0));
	}

	// turn a string date into a Date object
	// if passed in param is null/"", return null
	public static java.util.Date parseDateWithoutTime(String strDate) {
		if ( (strDate == null) || "".equals(strDate)) {
			return null;
		}

		return FORMATTER_WITHOUT_TIME.parse(strDate, new ParsePosition(0));
	}

	public static Timestamp parseTimestamp(String strStamp) {
		if ( (strStamp == null) || "".equals(strStamp)) {
			return null;
		}

		return Timestamp.valueOf(strStamp);
	}
//	public static String formatTime(String time){
//		String hour =""; 
//		String minu ="";
//		String newTime = "00:00";
//		int stat = 0;
//		int len = time.length();	
//		for(int i =0 ; i < len ;i++){
//			char temp = time.charAt(i);
//			if((':'==temp) || ('��'==temp)){//is space
//				stat = stat + 1;//change to minu status
//			}else{
//				if(0 == stat){
//					hour = hour + String.valueOf(temp);
//					log.info(">>>>the hour is : " + hour);
//				}else{
//					minu = minu + String.valueOf(temp);
//					log.info(">>>>the minu is : " + minu);
//				}
//
//			}
//		}
//		if(hour.length()==1){
//			hour = "0"+hour;
//		}
//		if(minu.length()==1){
//			minu = "0"+minu;
//		}
//		newTime = hour+":"+minu;
//		log.info(">>>>the new time is : " + newTime);
//		return newTime;
//	}

	public static int getDaysBetween (Date dd1, Date dd2){ 
		Calendar d1=Calendar.getInstance();
		d1.setTime(dd1);
		Calendar d2=Calendar.getInstance();
		d2.setTime(dd2);
		if (d1.after(d2)){    // swap dates so that d1 is start and d2 is end
			java.util.Calendar swap = d1;
			d1 = d2;
			d2 = swap;
		}
		int days = d2.get(Calendar.DAY_OF_YEAR) - d1.get(Calendar.DAY_OF_YEAR);
		int y2 = d2.get(Calendar.YEAR);
		if (d1.get(Calendar.YEAR) != y2){
			d1 = (Calendar) d1.clone();
			do{
				days += d1.getActualMaximum(Calendar.DAY_OF_YEAR);//�õ������ʵ������
				d1.add(Calendar.YEAR, 1);
			} while (d1.get(Calendar.YEAR) != y2);
		}
		return days;
	}
}


